﻿using DirectSell.Common;
using DirectSell.Model.Log;
using NLog;
using NLog.Config;
using NLog.Targets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DirectSell.Common.Log
{
    /// <summary>
    /// 日志记录工具类
    /// </summary>
    public static  class MyLog
    {
        /// <summary>
        /// 获取记录器
        /// </summary>
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        #region 日志记录--郭文昊--2018年10月7日20:41:34
        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="LogFile">文件名</param>
        /// <param name="LogFolder">文件夹</param>
        /// <param name="level">等级</param>
        public static void Log(object message, string LogFile,string LogFolder, MyLogLevel level)
        {
            message = TransModel<object>.GetEntityToString(message);
            //1.写入配置
            LogManager.Configuration= LogConfigLoad(LogFile, LogFolder);

            //2.记录日志
            switch (level)
            {
                case MyLogLevel.Debug: Logger.Debug(message);break;
                case MyLogLevel.Info: Logger.Info(message); break;
                case MyLogLevel.Error: Logger.Error(message); break;
                case MyLogLevel.Fatal: Logger.Fatal(message);break;
            }
        }
        #endregion

        #region NLog配置--郭文昊--2018年10月7日20:34:53
        /// <summary>
        /// Nlog配置
        /// </summary>
        /// <param name="LogFile">文件名</param>
        /// <param name="LogFolder"><文件夹/param>
        private static LoggingConfiguration LogConfigLoad(string LogFile,string LogFolder)
        {
            // Step 1. Create configuration object 

            LoggingConfiguration config = new LoggingConfiguration();

            // Step 2. Create targets and add them to the configuration 

            FileTarget fileTarget = new FileTarget();
            config.AddTarget("file", fileTarget);

            // Step 3. Set target properties 

            StringBuilder sb = new StringBuilder();

            fileTarget.FileName = string.Format("C:/LogInformation/${0}shortdate{1}/{2}/${3}level{4}_{5}.txt", "{", "}", LogFolder, "{", "}", LogFile);
            //fileTarget.Layout = @"${newline}date：	${date}
            //                    ${newline}level：	${level}${newline}logger：	${logger}
            //                    ${newline}machinename：	${machinename}
            //                    ${newline}message：	${message}
            //                    ${newline}appdomain：	${appdomain}
            //                    ${newline}assembly-version：	${assembly-version}
            //                    ${newline}basedir：	${basedir}
            //                    ${newline}callsite：	${callsite}
            //                    ${newline}callsite-linenumber：	${callsite-linenumber}
            //                    ${newline}counter：	${counter}${newline}nlogdir：	${nlogdir}
            //                    ${newline}processid：	${processid}
            //                    ${newline}processname：	${processname}
            //                    ${newline}specialfolder：	${specialfolder}
            //                    ${newline}stacktrace: ${stacktrace}
            //                    ${newline}-----------------------------------------------------------";
            fileTarget.Layout = @"${newline}date：	${date}
                                ${newline}level：	${level}
                                ${newline}message：	${message}
                                ${newline}stacktrace: ${stacktrace}
                                ${newline}-----------------------------------------------------------";
            // Step 4. Define rules 

            LoggingRule rule = new LoggingRule("*", LogLevel.Debug, fileTarget);
            config.LoggingRules.Add(rule);

            return config;
        }
        #endregion

    }
}
